# 链接网址  https://blog.csdn.net/qq_38410730/article/details/105093434
import xml.sax.handler

import matplotlib.pyplot as plt;
import numpy as np;
import scipy.optimize as opt;

# 函数，仅给出大致的曲线形状即可
def func(x, a, b, c):
     return a * np.exp(-b * x) + c

# 提供数据
x = []
for i in 100:
     x.append(i)
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
print(xdata)
print(ydata)
print(y)
print(y_noise)

# Plot the actual data
plt.plot(xdata, ydata, ".", label="Data");

# The actual curve fitting happens here
optimizedParameters, pcov = opt.curve_fit(func, xdata, ydata)

# Use the optimized parameters to plot the best fit
plt.plot(xdata, func(xdata, *optimizedParameters), label="fit");

# Show the graph
plt.legend();
plt.show();